package com.yu.flow.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class TaskConfig {

    private String name;
    private Function<WorkflowContext, ?> handler;
    private int retries = 0;
    private long retryDelay = 1000; // 毫秒
    private long timeout = 0; // 0表示不设置超时
    private boolean skipOnFailure = false;
    private List<String> dependencies = new ArrayList<>();
    private Map<String, String> inputMapping = new HashMap<>();
    private String outputKey;

    // 构造函数和builder模式
    private TaskConfig(String name, Function<WorkflowContext, ?> handler) {
        this.name = name;
        this.handler = handler;
    }

    public static Builder builder(String name, Function<WorkflowContext, ?> handler) {
        return new Builder(name, handler);
    }

    public String getName() {
        return this.name;
    }

    public Function<WorkflowContext, ?> getHandler() {
        return handler;
    }

    public int getRetries() {
        return retries;
    }

    public long getRetryDelay() {
        return retryDelay;
    }

    public long getTimeout() {
        return timeout;
    }

    public boolean isSkipOnFailure() {
        return skipOnFailure;
    }

    public List<String> getDependencies() {
        return dependencies;
    }

    public Map<String, String> getInputMapping() {
        return inputMapping;
    }

    public String getOutputKey() {
        return outputKey;
    }

    public static class Builder {
        private TaskConfig config;

        private Builder(String name, Function<WorkflowContext, ?> handler) {
            this.config = new TaskConfig(name, handler);
        }

        public Builder retries(int retries) {
            config.retries = retries;
            return this;
        }

        public Builder retryDelay(long retryDelay) {
            config.retryDelay = retryDelay;
            return this;
        }

        public Builder timeout(long timeout) {
            config.timeout = timeout;
            return this;
        }

        public Builder skipOnFailure(boolean skipOnFailure) {
            config.skipOnFailure = skipOnFailure;
            return this;
        }

        public Builder dependencies(List<String> dependencies) {
            config.dependencies = dependencies;
            return this;
        }

        public Builder inputMapping(Map<String, String> inputMapping) {
            config.inputMapping = inputMapping;
            return this;
        }

        public Builder outputKey(String outputKey) {
            config.outputKey = outputKey;
            return this;
        }

        public TaskConfig build() {
            return config;
        }


        // getters

    }
}